package controller;

import java.util.ArrayList;

import database.DBManager;
import database.LoadObject;
import database.MySQLDbManager;
import model.Aggregation;
import model.Association;
import model.Attribute;
import model.ClassObj;
import model.DomainModel;
import model.Inheritance;

public class DomainModelController {

	DomainModel domainModel;

	public DomainModelController(DomainModel dm) {
		domainModel = dm;
	}

	/**
	 * 
	 * @param selectedClass
	 * @return
	 */
	public ClassObj createClass(String selectedClass) {
		// first check to make sure there is no conflict with a current class
		ClassObj existingClass = domainModel.getAppropriateClass(selectedClass);
		if (existingClass != null) {
			return existingClass;
		}

		ClassObj classObj = new ClassObj();
		classObj.setName(selectedClass);
		domainModel.addClass(classObj);
		return classObj;
	}

	/**
	 * 
	 * @param selectedClass
	 * @param selectedAttribute
	 * @return
	 */
	public String createAttribute(String selectedClass, String selectedAttribute) {
		Attribute attribute = new Attribute();
		ClassObj classObj;
		attribute.setName(selectedAttribute);

		// if the class does not exist, create it
		classObj = createClass(selectedClass);
		classObj.addAttribute(attribute);
		domainModel.addAttribute(attribute);
		return selectedAttribute;
	}

	/**
	 * 
	 * @param srcClass
	 * @param destClass
	 * @param selectedAssociation
	 * @return
	 */
	public String createAssociation(String name, String srcClass,
			String destClass, String srcRole, String destRole,
			String srcMultiplicity, String destMultiplicity) {
		Association association = new Association();

		ClassObj srcClassObj = domainModel.getAppropriateClass(srcClass);
		ClassObj destClassObj = domainModel.getAppropriateClass(destClass);
		association.setName(name);
		association.setSrcClass(srcClassObj);
		association.setDestClass(destClassObj);
		association.setSrcRole(srcRole);
		association.setDestRole(destRole);
		association.setSrcMultiplicity(srcMultiplicity);
		association.setDestMultiplicity(destMultiplicity);
		domainModel.addAssociation(association);
		return name;
	}

	/**
	 * 
	 * @param srcClass
	 * @param destClass
	 * @param selectedAggregation
	 * @return
	 */

	public String createAggregation(String name, String srcClass,
			String destClass, String multiplicity) {
		Aggregation aggregation = new Aggregation();

		ClassObj srcClassObj = domainModel.getAppropriateClass(srcClass);
		ClassObj destClassObj = domainModel.getAppropriateClass(destClass);
		aggregation.setSrcClass(srcClassObj);
		aggregation.setDstClass(destClassObj);
		aggregation.setName(name);
		aggregation.setMultiplicity(multiplicity);

		domainModel.addAggregation(aggregation);
		return name;
	}

	/**
	 * 
	 * @param superClass
	 * @param subClass
	 * @return
	 */
	public String createInheritance(String superClass, String subClass) {
		Inheritance inheritance = new Inheritance();
		ClassObj srcClassObj = domainModel.getAppropriateClass(superClass);
		ClassObj destClassObj = domainModel.getAppropriateClass(subClass);
		inheritance.setSrcClass(srcClassObj);
		inheritance.setDstClass(destClassObj);
		domainModel.addInheritance(inheritance);

		return "Inheritance(" + superClass + " ," + subClass + ")";
	}

	/**
	 * 
	 * @param srcClass
	 * @param destClass
	 * @param assosciation
	 * @param role
	 * @return
	 */
	public String createRole(String role, String association, String srcClass,
			String destClass, boolean isSrc) {
		Association associationObj = (Association) domainModel.getAssociation(
				association, srcClass, destClass);

		if (isSrc) {
			associationObj.setSrcRole(role);
		} else {
			associationObj.setDestRole(role);
		}
		return role;
	}

	/**
	 * 
	 * @param srcClass
	 * @param destClass
	 * @param association
	 * @param role
	 * @return
	 */
	public String createAssociationMultiplicity(String multiplicity,
			String srcClass, String destClass, String association, boolean isSrc) {

		Association associationObj = (Association) domainModel.getAssociation(
				association, srcClass, destClass);

		if (isSrc) {
			associationObj.setSrcMultiplicity(multiplicity);
		} else {
			associationObj.setDestMultiplicity(multiplicity);
		}

		return multiplicity;
	}

	public String createAggregationMultiplicity(String multiplicity,
			String srcClass, String destClass, String aggregation) {

		Aggregation aggregationObj = (Aggregation) domainModel.getAggregation(
				aggregation, srcClass, destClass);

		aggregationObj.setMultiplicity(multiplicity);

		return multiplicity;
	}

	public ArrayList<ClassObj> getClassList() {
		return domainModel.getClassModelconceptList();
	}

	public ArrayList<Attribute> getAttributesOfClass(String className) {
		ClassObj classObj = domainModel.getAppropriateClass(className);
		if (classObj != null) {
			return classObj.getAttributes();
		} else {
			return new ArrayList<Attribute>();
		}
	}

	public ArrayList<Inheritance> getInheritanceList() {
		return domainModel.getInheritanceModelconceptList();
	}

	public ArrayList<Association> getAssociationList() {
		return domainModel.getAssociationModelconceptList();
	}

	public ArrayList<Aggregation> getAggregationList() {
		return domainModel.getAggregationModelconceptList();
	}

	/**
	 * 
	 * @return
	 */
	ArrayList<String> retrieveDocList() {
		DBManager db = MySQLDbManager.getInstance();
		ArrayList<String> docList = null;
		docList = db.RetrieveDocs();

		return docList;
	}

	public void savetoDB() {

		DBManager db = MySQLDbManager.getInstance();
		ArrayList<Attribute> attrList = null;
		ArrayList<Aggregation> aggrList = null;
		ArrayList<Association> assoList = null;
		ArrayList<Inheritance> inhrList = null;
		ArrayList<ClassObj> classList = null;

		classList = getClassList();
		int i = 0;
		ClassObj curClass = null;
		db.saveDocument("Test.txt");
		while (i < classList.size()) {
			curClass = classList.get(i);
			db.saveClass(curClass);

			String curClassName = curClass.getName();
			attrList = getAttributesOfClass(curClassName);
			System.out.println(curClassName);
			db.saveAttr(attrList, curClassName);
			i++;
		}

		aggrList = getAggregationList();
		db.saveAggr(aggrList);
		System.out.println("Saved Aggregation");
		assoList = getAssociationList();
		db.saveAsso(assoList);
		inhrList = getInheritanceList();
		db.saveInhr(inhrList);

	}

	/**
	 * 
	 * @return
	 */
	public void loadFromDB() {
		DBManager db = MySQLDbManager.getInstance();

		String docName = "Test.txt";
		db.loadDocument(docName);
		db.loadClass();
		db.loadAttribute();
		db.loadAggregation();
		db.loadAssociation();
		db.loadInheritance();
		LoadObject lb = new LoadObject();
		lb = db.loadDomainModelInfo();

		//domainModel.setName();
		//domainModel.setClassModelconceptList();	 
		//domainModel.setAggregationModelconceptList();
		//domainModel.setAssociationModelconceptList();
		//domainModel.setInheritanceModelconceptList();
		
	}

}
